import {Graph, Node} from '@antv/x6'
import { Transform } from '@antv/x6-plugin-transform'
import { useX6Store } from "@/views/ai/agent/x6/store/x6";
import { Snapline } from "@antv/x6-plugin-snapline";
import { Clipboard } from "@antv/x6-plugin-clipboard";
import { Keyboard } from "@antv/x6-plugin-keyboard";
import { Scroller } from "@antv/x6-plugin-scroller";
import { Export } from '@antv/x6-plugin-export'
import { History } from "@antv/x6-plugin-history";
import { Selection } from "@antv/x6-plugin-selection";
import BluePrint from "./node/BluePrint.vue";
import { register } from '@antv/x6-vue-shape'
import {ShapeName} from './node/types'
import AiNode from "@/views/ai/agent/x6/node/AiNode.vue";
import AiIntention from "@/views/ai/agent/x6/node/AiIntention.vue";
import ValueInput from "@/views/ai/agent/x6/node/ValueInput.vue";
import {defaultGroup} from "@/views/ai/agent/x6/tools";
import FileLoad from "@/views/ai/agent/x6/node/FileLoad.vue";
import TextChunk from "@/views/ai/agent/x6/node/TextChunk.vue";
import MarkingTag from "@/views/ai/agent/x6/node/marking-tag.vue";
import Scoring from "@/views/ai/agent/x6/node/Scoring.vue";
import McpAnalyzingResume from "@/views/ai/agent/x6/mcp/mcp-analyzing-resume.vue";
import ContentClassify from "@/views/ai/agent/x6/node/content-classify.vue";
import QdrantNode from "@/views/ai/agent/x6/node/QdrantNode.vue";
import QdrantQuery from "@/views/ai/agent/x6/node/QdrantQuery.vue";
import DocumentTransform from "@/views/ai/agent/x6/node/DocumentTransform.vue";
import ValueOutput from "@/views/ai/agent/x6/node/ValueOutput.vue";
import Multimodal from "@/views/ai/agent/x6/node/Multimodal.vue";
import UploadFile2ai from "@/views/ai/agent/x6/node/UploadFile2ai.vue";
import IfNode from '@/views/ai/agent/x6/node/IfNode.vue'
import RandomNode from '@/views/ai/agent/x6/node/RandomNode.vue'
import FileType from "@/views/ai/agent/x6/node/FileType.vue";

import Constant from "@/views/ai/agent/x6/node/Constant.vue";
import Save2es from "@/views/ai/agent/x6/node/Save2es.vue";
import Query2es from "@/views/ai/agent/x6/node/Query2es.vue";

export const initPlug=(graph:Graph)=>{
  const x6=useX6Store()
  graph.use(
    new Transform(x6.transformConfig)
  )
  graph.use(
    new Snapline(x6.snapLineConfig)
  )
  graph.use(
    new Clipboard(x6.clipboardConfig)
  )
  graph.use(
    new Keyboard(x6.keyboardConfig)
  )
  graph.use(
    new History(x6.historyConfig)
  )
  graph.use(
    //@ts-ignore
    new Selection(x6.selectConfig)
  )
  graph.use(
    new Export()
  )
  graph.on('cell:mouseenter', ({ cell }) => {
    if (cell.isNode()) {
      cell.addTools([{name: 'button-remove',},])
    }
  })
  graph.on('cell:mouseleave', ({ cell }) => {
    cell.removeTools()
  })
  graph.on('edge:mouseenter',({edge})=>{
    edge.addTools([{name:'button-remove'},{name:'vertices'}])
  })
  graph.on('edge:mouseleave', ({ edge }) => {
    edge.removeTools()
  })
  graph.on('edge:removed',({edge})=>{
    //console.log('removed edge',edge.getData())
    const type=edge.getData()?.type
    if (type !== 'exec'&&edge.getData()){
      const targetId=edge.getData().target
      const targetNode=graph.getCellById(targetId)
      if (targetNode){
        const d=targetNode.getData()
        //console.log('target node data',d)
        if (d&&d['input']){
          d['input']=d['input'].filter(l=>l.link !== edge.id)
          console.log('target node data',d['input'])
          targetNode.setData(d,{deep:true,overwrite:true})
        }
      }
    }
  })
  graph.on('edge:connected',({edge})=>{
    const d=edge.getData()
    const type=d?.type
    if (type !== 'exec'&& d){
      const targetId=d.target
      const targetCell=graph.getCellById(targetId)
      const d2=targetCell.getData()||{}
      if (!d2['input']){
        d2['input']=[]
      }
      d2['input'].push({source:d.source,key:d.key,link:edge?.id,dot:d.targetType,alias:d.alias})
      targetCell.setData(d2,{deep:true,overwrite:true})
    }
  })
}
export const initRegister=()=>{
  register({
    shape:ShapeName.BLUE_PRINT,
    width: 200,
    component: BluePrint,
    zIndex:100
  })
  register({
    shape:ShapeName.LLM_EXEC,
    width:400,
    component: AiNode,
    zIndex:100
  })
  register({
    shape:ShapeName.INTENTION,
    width:400,
    component:AiIntention,
    zIndex:100
  })
  register({
    shape:ShapeName.VALUE_INPUT,
    width:200,
    component:ValueInput,
    zIndex:100
  })
  register({
    shape:ShapeName.FILE_LOADER,
    component:FileLoad,
    zIndex:100
  })
  register({
    shape:ShapeName.TEXT_CHUNK,
    component:TextChunk,
    zIndex:100
  })
  register({
    shape:ShapeName.MARKING_TAG,
    component:MarkingTag,
    zIndex:100
  })
  register({
    shape:ShapeName.SCORING,
    component:Scoring,
    zIndex:100
  })
  register({
    shape:ShapeName.MCP_ANALYZING_RESUME,
    component:McpAnalyzingResume,
    zIndex:100
  })
  register({
    shape:ShapeName.CONTENT_CLASSIFY,
    component: ContentClassify,
    zIndex:100
  })
  register({
    shape:ShapeName.DATABASE_QDRANT,
    component:QdrantNode,
    zIndex:100
  })
  register({
    shape:ShapeName.QUERY_QDRANT,
    component:QdrantQuery,
    zIndex:100
  })
  register({
    shape:ShapeName.DOCUMENT_TRANSFORM,
    component:DocumentTransform,
    zIndex:100
  })
  register({
    shape:ShapeName.VALUE_OUTPUT,
    component:ValueOutput,
    zIndex:100
  })
  register({
    shape:ShapeName.MULTIMODAL,
    component:Multimodal,
    zIndex:100
  })
  register({
    shape:ShapeName.UPLOAD_FILE2AI,
    component: UploadFile2ai,
    zIndex:100
  })
  register({
    shape:ShapeName.IF_ELSE,
    component:IfNode,
    zIndex:100
  })
  register({
    shape:ShapeName.RANDOM_NODE,
    component:RandomNode,
    zIndex:100
  })
  register({
    shape:ShapeName.FILE_TYPE,
    component:FileType,
    zIndex:100
  })
  register({
    shape:ShapeName.CONSTANT,
    component:Constant,
    zIndex:100
  })
  register({
    shape:ShapeName.SAVE_2ES,
    component:Save2es,
    zIndex:100
  })
  register({
    shape:ShapeName.QUERY_2ES,
    component:Query2es,
    zIndex:100
  })
}
